<?php

class imageDraw {

	
	/**
	* set variables with constructor
	*	default 5px corner radii, 5px margin, 100px width
	*/
	function imageDraw($r = 5,$m = 5, $w = 100, $h = 100){
	
		$this->r = $r;
		$this->m = $m;
		$this->w = $w;
		$this->h = $h;
		$this->canvas = @imagecreatetruecolor($this->w,$this->h);
		imagesavealpha($this->canvas, true);
		$trans_colour = imagecolorallocatealpha($this->canvas, 0, 0, 0, 127);
   		imagefill($this->canvas, 0, 0, $trans_colour);
		
	}
	
	function setColor(&$canvas,$hexcolor){
		$hexcolor = strtoupper( preg_replace('/#/','',$hexcolor) );
	//	var_dump($hexcolor);
		$a = hexdec(substr($hexcolor,0,2));
		$b = hexdec(substr($hexcolor,2,2));
		$c = hexdec(substr($hexcolor,4,2));
		
		$color = imagecolorallocate($canvas,$a,$b,$c);
		//$color = imagecolorallocate($canvas,0xCC, 0xCC, 0xCC);
		/*
		var_dump(array('0x'.substr($hexcolor,0,2),
						'0x'.substr($hexcolor,2,2),
						'0x'.substr($hexcolor,4,2)));
		*/
		return $color;
	}
	
	function mkCorners($ellipse_color){
		for($i=1;$i<=4;$i++){
	 		$x = 'x'.$i;
			$y = 'y'.$i;
			//$this->imagefilledellipseaa($this->canvas, $this->p[$x], $this->p[$y], 2 * $this->r, 2 * $this->r, $ellipse_color);
			imagefilledellipse($this->canvas, $this->p[$x], $this->p[$y], 2 * $this->r, 2 * $this->r, $ellipse_color);
		}//end for
	}//end mkCorners function
	
	function mkPoly8($octogon_color){
		imagefilledpolygon($this->canvas, $this->pp, 8, $octogon_color);
	}
	/**
	* define the 12 points necessary for our round corner rectangle
	*/
	function mkPoints(){
	
		$this->p = array(		
				x1 => $this->m + $this->r -1,
				y1 => $this->m + $this->r -1,
				x2 => $this->w - ($this->m + $this->r) -1,
				y2 => $this->m + $this->r -1,
				x3 => $this->m + $this->r -1,
				y3 => $this->m + $this->r + 60 -1,
				x4 => $this->w - ($this->m + $this->r) -1,
				y4 => $this->m + $this->r + 60 -1
			);
		$this->poly = array(
				x5 => $this->m -1,
				y5 => $this->m + $this->r -1,
				x6 => $this->m + $this->r -1,
				y6 => $this->m -1,	
				x7 => $this->w - ($this->m + $this->r) -1,
				y7 => $this->m -1,
				x8 => $this->w - $this->m -1,
				y8 => $this->m + $this->r -1,				
				x9 => $this->w - $this->m -1,
				y9 => $this->m + $this->r + 60 -1,
				x10 => $this->w - ($this->m + $this->r) -1,
				y10 => $this->m + (2 * $this->r) + 60 -1,			
				x11 => $this->m + $this->r -1,
				y11 => $this->m + (2 * $this->r) + 60 -1,
				x12 => $this->m -1,
				y12 => $this->m + $this->r + 60 -1
		);
		
		$this->pp = array_values($this->poly);
	}//end mkPoints
	
	//////////////////////////////////////////

	// Parses a color value to an array.
	function color2rgb($color)
	{
		$this->rgb = array();
			
		$this->rgb[] = 0xFF & ($color >> 16);
		$this->rgb[] = 0xFF & ($color >> 8);
		$this->rgb[] = 0xFF & ($color >> 0);
	
		return $this->rgb;
	}
	
	// Parses a color value to an array.
	function color2rgba($color)
	{
		$this->rgb = array();
			
		$this->rgb[] = 0xFF & ($color >> 16);
		$this->rgb[] = 0xFF & ($color >> 8);
		$this->rgb[] = 0xFF & ($color >> 0);
		$this->rgb[] = 0xFF & ($color >> 24);
	
		return $this->rgb;
	}
	
	// Adapted from http://homepage.smc.edu/kennedy_john/BELIPSE.PDF
	function imagefilledellipseaa_Plot4EllipsePoints(&$im, $CX, $CY, $X, $Y, $color, $t)
	{
		imagesetpixel($im, $CX+$X, $CY+$Y, $color); //{point in quadrant 1}
		imagesetpixel($im, $CX-$X, $CY+$Y, $color); //{point in quadrant 2}
		imagesetpixel($im, $CX-$X, $CY-$Y, $color); //{point in quadrant 3}
		imagesetpixel($im, $CX+$X, $CY-$Y, $color); //{point in quadrant 4}
	
		$aColor = $this->color2rgba($color);
		$this->mColor = imagecolorallocate($im, $aColor[0], $aColor[1], $aColor[2]);
		if ($t == 1)
		{
			imageline($im, $CX-$X, $CY-$Y+1, $CX+$X, $CY-$Y+1, $this->mColor);
			imageline($im, $CX-$X, $CY+$Y-1, $CX+$X, $CY+$Y-1, $this->mColor);
		} else {
			imageline($im, $CX-$X+1, $CY-$Y, $CX+$X-1, $CY-$Y, $this->mColor);
			imageline($im, $CX-$X+1, $CY+$Y, $CX+$X-1, $CY+$Y, $this->mColor);
		}
		imagecolordeallocate($im, $this->mColor);
	}
	// Adapted from http://homepage.smc.edu/kennedy_john/BELIPSE.PDF
	function imagefilledellipseaa(&$im, $CX, $CY, $width, $Height, $color)
	{
	
		$XRadius = floor($width/2);
		$YRadius = floor($Height/2);
	
		$baseColor = $this->color2rgb($color);
	
		$TwoASquare = 2*$XRadius*$XRadius;
		$TwoBSquare = 2*$YRadius*$YRadius;
		$X = $XRadius;
		$Y = 0;
		$XChange = $YRadius*$YRadius*(1-2*$XRadius);
		$YChange = $XRadius*XRadius;
		$EllipseError = 0;
		$StoppingX = $TwoBSquare*$XRadius;
		$StoppingY = 0;
	
		$alpha = 77;	
		$color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha);
		//$color = imagecolorexactalpha($im, 0, 0, 0, $alpha);
		
		while ($StoppingX >= $StoppingY) // {1st set of points, y' > -1}
		{
			$this->imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 0);
			$Y++;
			$StoppingY += $TwoASquare;
			$EllipseError += $YChange;
			$YChange += $TwoASquare;
			if ((2*$EllipseError + $XChange) > 0)
			{
				$X--;
				$StoppingX -= $TwoBSquare;
				$EllipseError += $XChange;
				$XChange += $TwoBSquare;
			}
			
			// decide how much of pixel is filled.
			$filled = $X - sqrt(($XRadius*$XRadius - (($XRadius*$XRadius)/($YRadius*$YRadius))*$Y*$Y));
			$alpha = abs(90*($filled)+37);
			imagecolordeallocate($im, $color);
			//$color = imagecolorexactalpha($im,255, 255, 255, $alpha);
			$color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha);
		}
		// { 1st point set is done; start the 2nd set of points }
	
		$X = 0;
		$Y = $YRadius;
		$XChange = $YRadius*$YRadius;
		$YChange = $XRadius*$XRadius*(1-2*$YRadius);
		$EllipseError = 0;
		$StoppingX = 0;
		$StoppingY = $TwoASquare*$YRadius;
		$alpha = 77;	
		$color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha);
		
		while ($StoppingX <= $StoppingY) // {2nd set of points, y' < -1}
		{
			$this->imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 1);
			$X++;
			$StoppingX += $TwoBSquare;
			$EllipseError += $XChange;
			$XChange += $TwoBSquare;
			if ((2*$EllipseError + $YChange) > 0)
			{
				$Y--;
				$StoppingY -= $TwoASquare;
				$EllipseError += $YChange;
				$YChange += $TwoASquare;
			}
			
			// decide how much of pixel is filled.
			$filled = $Y - sqrt(($YRadius*$YRadius - (($YRadius*$YRadius)/($XRadius*$XRadius))*$X*$X));
			$alpha = abs(90*($filled)+37);
			imagecolordeallocate($im, $color);
			$color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha);
		}
		
	}
	
}//end imageDraw class
?>